/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.entity.PartAddress;
import org.springblade.modules.repair.service.IPartAddressService;
import org.springblade.modules.repair.wrapper.PartAddressWrapper;
import org.springframework.web.bind.annotation.*;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 配件收货地址 控制器
 *
 * @author ysq
 * @since 2023-09-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("repair/partaddress")
@Api(value = "配件收货地址", tags = "配件收货地址接口")
public class PartAddressController extends BladeController {

	private final IPartAddressService partAddressService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入partAddress")
	public R<PartAddress> detail(Long planId) {
		if (planId == null || planId == 0) {
			PartAddress detail = partAddressService.getOne(Wrappers.<PartAddress>lambdaQuery().eq(PartAddress::getPlanId, 0).orderByDesc(PartAddress::getCreateTime).last("limit 1"));
			return R.data(detail);
		} else {
			final List<PartAddress> list = partAddressService.list(Wrappers.<PartAddress>lambdaQuery().eq(PartAddress::getPlanId, planId));
			if (list.size() > 0) {
				return R.data(list.get(0));
			}
			return R.data(null);
		}
	}


	/**
	 * 新增或修改 配件收货地址
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入partAddress")
	public R submit(@Valid @RequestBody PartAddress partAddress) {
		return R.status(partAddressService.saveOrUpdate(partAddress));
	}


}
